package com.zzptc.zzsgz.bdsjws.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.base.BaseActivity;
import com.zzptc.zzsgz.bdsjws.fragments.SelectContactFragment;
import com.zzptc.zzsgz.bdsjws.fragments.ShowAllContactFragment;
import com.zzptc.zzsgz.bdsjws.utils.MyUtils;
import com.zzptc.zzsgz.bdsjws.vo.Contact;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AddContactsActivity extends BaseActivity {
    SelectContactFragment selectContactFragment;
    ShowAllContactFragment showAllContactFragment;//第一个碎片
    @BindView(R.id.et_contact)
    EditText etContact;
    @BindView(R.id.tv_count)
    TextView tvCount;

    boolean flag=true;
    List<Contact> list;
    List<Contact> selectlist=new ArrayList<Contact>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public int getContentXML() {
        return R.layout.activity_add_contacts;
    }

    @Override
    public void initData() {
        tvCount.setText("("+ MyApplication.getSOSList().size()+")");
        // 在Fragment中绑定数据源 查询内容提供者  查询  联系人信息 URI:  ContactsContract.Contacts.CONTENT_URI
        //List<Contact> lisContact = MyUtils.getAllContacts();
        //绑定 RecycleView
        //用fragment来替换Fragmelayout
       list = MyApplication.getList();
        if (showAllContactFragment == null) {
            showAllContactFragment = new ShowAllContactFragment();
        }
        getFragmentManager().beginTransaction().replace(R.id.fl_show_contacts, showAllContactFragment)
                .addToBackStack("contact").commit();

    }

    /**
     *
     */
    public void onBackPressed() {
        super.onBackPressed();
        if (getFragmentManager().getBackStackEntryCount() > 1) {
            getFragmentManager().popBackStack();
        } else {
            finish();
        }
    }

    @Override
    public void intView() {

        etContact.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (flag) {
                    if (selectContactFragment == null) {
                        selectContactFragment = new SelectContactFragment();
                    }
                    //替换碎片Fragment
                    if (!selectContactFragment.isAdded()){
                        getFragmentManager().beginTransaction().hide(showAllContactFragment)
                                .add(R.id.fl_show_contacts, selectContactFragment).commit();
                    }
                    flag=false;
                }
            }

            @Override
            public void onTextChanged(final CharSequence charSequence, int i, int i1, int i2) {
              final  CharSequence c=charSequence.toString().replace(" ","").replace("-","");
                Toast.makeText(AddContactsActivity.this, charSequence, Toast.LENGTH_SHORT).show();
                selectlist.clear();
                //项 适配器
                //耗时工作放入线程
                 new Thread(new Runnable() {
                     @Override
                     public void run() {
                         for (int j = 0; j < list.size(); j++) {
                             Contact contact = list.get(j);
                             if (contact.getName().contains(c) || contact.getPhonenum().toString()
                                     .replace(" ", "").replace("-", "").contains(c)) {
                                 //产生一个新x的集合 true匹配上了 false没有匹配的电话号码或联系人姓名
                                 selectlist.add(contact);
                             }
                         }
                         //循环完毕之后 更新第二个碎片
                        // selectContactFragment.getSelectContactList(selectlist);
                         runOnUiThread(new Runnable() {
                             @Override
                             public void run() {
                                 selectContactFragment.getSelectContactList(selectlist);
                             }
                         });
                     }
                 }).start();
            }

            @Override
            public void afterTextChanged(Editable editable) {

                if(etContact.getText().toString().length()==0){
                   if (getFragmentManager().getBackStackEntryCount() > 1){
                       getFragmentManager().popBackStack();
                   }
                   flag=true;//开关再次打开
               }
            }

        });

        //监听事件
//        etContact.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//                if (flag) {
//                    if (selectContactFragment == null) {
//                        selectContactFragment = new SelectContactFragment();
//                    }
//                    //替换碎片Fragment
//                    getFragmentManager().beginTransaction().hide(showAllContactFragment)
//                            .add(R.id.fl_show_contacts, selectContactFragment).commit();
//                    flag=false;
//                }
//            }
//
//
//            @Override
//            public void onTextChanged(final CharSequence charSequence, int i, int i1, int i2) {
//              final  CharSequence c=charSequence.toString().replace(" "," ").replace("-","");
//                Toast.makeText(AddContactsActivity.this, charSequence, Toast.LENGTH_SHORT).show();
//                selectlist.clear();
//                //项 适配器
//                //耗时工作放入线程
//                 new Thread(new Runnable() {
//                     @Override
//                     public void run() {
//                         for (int j = 0; j < list.size(); j++) {
//                             Contact contact = list.get(j);
//                             if (contact.getName().contains(c) || contact.getPhonenum().toString()
//                                     .replace(" ", "").replace("-", "").contains(c)) {
//                                 //产生一个新的集合 true匹配上了 false没有匹配的电话号码或联系人姓名
//                                 selectlist.add(contact);
//                             }
//                         }
//                         //循环完毕之后 更新第二个碎片
//                        // selectContactFragment.getSelectContactList(selectlist);
//                         runOnUiThread(new Runnable() {
//                             @Override
//                             public void run() {
//                                 selectContactFragment.getSelectContactList(selectlist);
//                             }
//                         });
//                     }
//                 }).start();
//            }
//            @Override
//            public void afterTextChanged(Editable editable) {
//               if(etContact.getText().length()==0){
//                   if (getFragmentManager().getBackStackEntryCount() > 1){
//                       getFragmentManager().popBackStack();
//                   }
//                   flag=true;//开关再次打开
//               }
//            }
//        });



    }

    @OnClick(R.id.img_add)
    public void onViewClicked() {
        startActivity(new Intent(this, OneKeySOSActivity.class));
    }

}
